package com.zhu.tree;


import java.util.LinkedList;
import java.util.Queue;

/**
 * 二分搜索树
 * @author: zhu
 * @date: 2019/1/14 16:10
 */
public class BST<E extends Comparable<E>> {

    private class Node{//节点类
        public E e;//用来存储元素
        public Node left,right;//左孩子，右孩子
        public Node(E e){
            this.e = e;
            left = null;
            right = null;
        }
    }

    private Node root;//根节点
    private int size;//元素的个数
    public BST(){
        root = null;
        size = 0;
    }

    /** 获取二分搜索树中元素的个数 */
    public int size(){
        return size;
    }

    /** 判断二分搜索树是否为空 */
    public boolean isEmpty(){
        return size == 0;
    }

    /** 插入方法，返回插入新节点后二分搜索树的根(仍然使用递归) */
    private Node add(Node node,E e){
        if (node == null){
            size ++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0){
            node.left = add(node.left,e);
        }else if (e.compareTo(node.e) > 0){
            node.right = add(node.right,e);
        }
        return node;
    }

    /** 向二分搜索树中添加元素 */
    public void add(E e){
        root = add(root,e);
    }

    /** 判断二分搜索树中是否包含元素e */
    public boolean contains(E e){
        return contains(root,e);
    }


    /** 查询元素 */
    private boolean contains(Node node,E e){
        if (node == null){
            return false;
        }
        if (e.compareTo(node.e) == 0){
            return true;
        }else if (e.compareTo(node.e) < 0){
            return contains(node.left,e);
        }else
            return contains(node.right,e);
    }

    /** 二分搜索树的前序遍历 */
    public void preOrder(){
        preOrder(root);
    }

    private void preOrder(Node node){
        if (node == null)
            return;
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    /** 二分搜索树中序遍历(遍历出来的就是排好序的) */
    public void inOrder(){
        inOrder(root);
    }

    private void inOrder(Node node){
        if (node == null)
            return;
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    /** 二分搜索树后序遍历 */
    public void postOrder(){
        postOrder(root);
    }

    private void postOrder(Node node){
        if (node == null)
            return;
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    /** 二分搜索树层序遍历 */
    public void levelOrder(){
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            Node cur = queue.remove();
            System.out.println(cur.e);
            if (cur.left != null)
                queue.add(cur.left);
            if (cur.right != null)
                queue.add(cur.right);
        }
    }

    /** 寻找二分搜索树的最小值 */
    public E minele(){
        if (size == 0)
            throw new IllegalArgumentException("二分搜索树为空");
        return minele(root).e;
    }

    private Node minele(Node node){
        if (node.left == null)
            return node;
        return minele(node.left);
    }

    /** 寻找二分搜索树的最大值 */
    public E maxele(){
        if (size == 0)
            throw new IllegalArgumentException("二分搜索树为空");
        return maxele(root).e;
    }

    private Node maxele(Node node){
        if (node.right == null)
            return node;
        return maxele(node.right);
    }

    /** 从二分搜索树中删除最小元素，并返回该元素 */
    public E removeMin(){
        E temp = minele();//最小元素
        root = removeMin(root);//删除最小元素
        return temp;//返回最小元素
    }

    private Node removeMin(Node node){
        if (node.left == null){
            Node rightNode = node.right;//保留右边的
            node.right = null;
            size --;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    /** 从二分搜索树中删除最大元素，并返回该元素 */
    public E removeMax(){
        E temp = minele();//最小元素
        root = removeMax(root);//删除最小元素
        return temp;//返回最小元素
    }

    private Node removeMax(Node node){
        if (node.right == null){
            Node leftNode = node.left;//保留右边的
            node.left = null;
            size --;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    /** 删除二分搜索树中的元素e */
    public void remove(E e){
        root = remove(root,e);
    }

    private Node remove(Node node,E e){
        if (node == null)
            return null;
        if (e.compareTo(node.e) < 0){
            node.left = remove(node.left,e);
            return node;
        }else if (e.compareTo(node.e) > 0){
            node.right = remove(node.right,e);
            return node;
        }else {
            if (node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size --;
                return rightNode;
            }
            if (node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size --;
                return leftNode;
            }
            Node successor = minele(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            node.left = node.right = null;
            return successor;
        }
    }

}
